<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>

<script>
    let createXHR = function () {
        if (typeof XMLHttpRequest !== 'undefined') {
            return new XMLHttpRequest();
        } else if (typeof ActiveXObject !== 'undefined') {
            if (typeof arguments.callee.activeXString !== 'string') {
                let versions = ['MSXML2.XMLHttp.6.0', 'MSXML2.XMLHttp.3.0', 'MSXML2.XMLHttp'],
                    i = 0,
                    len = versions.length;
                for (; i < len; i++) {
                    new ActiveXObject(versions[i]);
                    arguments.callee.activeXString = versions[i];
                }
            }
        } else {
            throw new Error('您的浏览器不支持Ajax.');
        }
    };

    /*
    * 第一种方案
    * 第一次加载的时候损失性能， 但是第一次调用的时候不损失性能
    * */
    let createXHR1 = (function () {
        if (typeof XMLHttpRequest !== 'undefined') {
            return function () {
                return new XMLHttpRequest();
            }
        } else if (typeof ActiveXObject !== 'undefined') {
            return function () {
                if (typeof arguments.callee.activeXString !== 'string') {
                    let versions = ['MSXML2.XMLHttp.6.0', 'MSXML2.XMLHttp.3.0', 'MSXML2.XMLHttp'],
                        i = 0,
                        len = versions.length;
                    for (; i < len; i++) {
                        new ActiveXObject(versions[i]);
                        arguments.callee.activeXString = versions[i];
                    }
                }
            }
        } else {
            return function () {
                throw new Error('您的浏览器不支持Ajax.');
            }
        }
    })();

    /*
    * 第二种方案： 惰性加载
    * 加载时不损失性能， 第一次调用的时候损失性能
    * */
    let createXHR2 = function () {
        if (typeof XMLHttpRequest !== 'undefined') {
            createXHR2 = function () {
                return new XMLHttpRequest();
            }
        } else if (typeof ActiveXObject !== 'undefined') {
            createXHR2 =  function () {
                if (typeof arguments.callee.activeXString !== 'string') {
                    let versions = ['MSXML2.XMLHttp.6.0', 'MSXML2.XMLHttp.3.0', 'MSXML2.XMLHttp'],
                        i = 0,
                        len = versions.length;
                    for (; i < len; i++) {
                        new ActiveXObject(versions[i]);
                        arguments.callee.activeXString = versions[i];
                    }
                }
            }
        } else {
            createXHR2 = function () {
                throw new Error('您的浏览器不支持Ajax.');
            }
        }
        return createXHR2;
    }
</script>
</body>
</html>